package itsm.isperp.module.controller;

import java.io.File;
import java.io.IOException;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.utils.FileUtils;
import itsm.isperp.framework.utils.ImageUtils;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.service.app.AppIconService;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * 框架中标签库的控制器
 * 
 * @author lizx
 * @date 2013-3-16
 */
@Controller
@RequestMapping("tags")
public class TagsController {

	@RequestMapping("selector/treeSelector")
	public ModelAndView treeSelector(String ctId, String url, String height) {
		ModelAndView mv = new ModelAndView("tags/selector/treeSelector");
		return mv;
	}

	@RequestMapping("selector/departmentUserSelector")
	public ModelAndView departmentUserSelector(String ctId, String url,
			String dheight, String idFieldName, HttpServletRequest req) {

		ModelAndView mv = new ModelAndView(
				"tags/selector/departmentUserSelector");

		mv.addObject("ctId", ctId);
		mv.addObject("dheight", dheight);

		String multiselect = req.getParameter("multiselect");
		mv.addObject("multiselect", multiselect);
		
		String roleName=req.getParameter("roleName");
		mv.addObject("roleName", roleName);

		if (StringUtils.isEmpty(idFieldName)) {
			idFieldName = "name";
		}
		String ifIncludeNoRoleUser=req.getParameter("ifIncludeNoRoleUser");
		mv.addObject("ifIncludeNoRoleUser", ifIncludeNoRoleUser);
		
		String parentIdControlId = req.getParameter("parentIdControlId");
    String ifasyncLoad = "{asyncLoad:1}";
    if (StringUtils.isNotEmpty(parentIdControlId)) {
      ifasyncLoad = "{asyncLoad:0}";
    }
    mv.addObject("ifasyncLoad", ifasyncLoad);
    mv.addObject("parentIdControlId", parentIdControlId);
		
		mv.addObject("idFieldName", idFieldName);
		
		String projectIdForPrjMember = req.getParameter("projectIdForPrjMember");
		mv.addObject("projectIdForPrjMember", projectIdForPrjMember);
		
		return mv;
	}

	@RequestMapping("selector/iconSelector")
	public ModelAndView treeSelector(String ctId, String size) {
		ModelAndView mv = new ModelAndView("tags/selector/iconSelector");

		AppIconService appIconService = ContextHolder
				.getSpringBean("appIconService");
		JqGridRequest req = new JqGridRequest();
		req.isPageable(false);

		if (StringUtils.isEmpty(size)) {
			size = "16x16";
		}

		req.addFilterRule("size", JqGridFilterRuleOp.eq, size);

		switch (size) {
		case "16x16":
			mv.addObject("checked16", "checked='checked'");

			break;

		case "24x24":
			mv.addObject("checked24", "checked='checked'");
			break;
		case "48x48":
			mv.addObject("checked48", "checked='checked'");
			break;
		}

		mv.addObject("icons", appIconService.findAll(req));
		mv.addObject("ctId", ctId);
		return mv;
	}

	@RequestMapping("selector/listSelector")
	public ModelAndView listSelector(String ctId, String url, String dheight,
			String colModel, String colNames, HttpServletRequest req) {
		ModelAndView mv = new ModelAndView("tags/selector/listSelector");

		String multiselect = req.getParameter("multiselect");

		String showPager = req.getParameter("showPager");

		boolean s = false;
		int rowNum = 1000;
		if (StringUtils.isEmpty(showPager) || "true".equals(showPager.trim())) {
			s = true;
			rowNum = 10;
		}

		mv.addObject("multiselect", multiselect);

		mv.addObject("ctId", ctId);
		mv.addObject("url", url.replace("|", "&"));

		mv.addObject("dheight", dheight);
		mv.addObject("colModel", colModel);
		mv.addObject("colNames", colNames);
		mv.addObject("showPager", s);
		mv.addObject("rowNum", rowNum);

		if ("true".equals(multiselect)) {
			mv.addObject("multiselect", true);
		} else {
			mv.addObject("multiselect", false);
		}
		return mv;

	}

	/**
	 * 
	 * @param imgSrc
	 * @param dwidth
	 * @param dheight
	 * @param cutType
	 *            1表示切图，2表示使用缩略图，0表示不做任何处理
	 * @param req
	 * @return
	 */
	@RequestMapping("uploader/photoEdit")
	public ModelAndView photoEdit(String imgSrc, String imgWidth,
			String imgHeight, String cutType, String imageFolder,
			HttpServletRequest req) {
		ModelAndView mv = new ModelAndView("tags/uploader/photoEdit");

		if (StringUtils.isEmpty(cutType)) {
			cutType = "1";
		}

		mv.addObject("imgSrc", imgSrc);
		mv.addObject("dwidth", imgWidth);
		mv.addObject("dheight", imgHeight);
		mv.addObject("cutType", cutType);

		mv.addObject("imageFolder", imageFolder);
		mv.addObject("imgPath", req.getParameter("imgPath"));

		mv.addObject("dialogId", req.getParameter("dialogId"));
		return mv;

	}

	@ResponseBody
	@RequestMapping("uploader/savePhoto")
	public ResultMessage savePhoto(String imgSrc, Integer x, Integer y,
			Integer x2, Integer y2, Integer w, Integer h, Integer dwidth,
			Integer dheight, String cutType, String imageFolder,
			HttpServletRequest req) throws IOException {
		String path = ContextHolder.getApplicationPath() + imgSrc;
		String imgPath = req.getParameter("imgPath");

		File f = new File(path);
		if (!f.exists()) {
			return ResultMessage.failure("源文件不存在！");
		}
		String aImgPath = "global/img/";
		if (StringUtils.isEmpty(imgPath))
			aImgPath = aImgPath + FileUtils.generateFilename(f.getName());
		else
			aImgPath = aImgPath + imgPath
					+ FileUtils.getFileExtname(f.getName());

		String createImgPath = ContextHolder.getUploadFileStorePath()
				+ aImgPath;
		if ("1".equals(cutType)) {
			ImageUtils.abscut(path, createImgPath, x, y, w, h);
			new File(path).delete();

		} else if ("2".equals(cutType)) {
			ImageUtils.scale(path, createImgPath, dwidth, dheight);
			new File(path).delete();
		} else {

			File fFile = new File(ContextHolder.getUploadFileStorePath()
					+ aImgPath);

			boolean ok = FileUtils.copyFile(new File(path), fFile);
			if (ok) {
				new File(path).delete();
			}
		}

		return ResultMessage.success(ContextHolder.getUploadFileStore()
				+ aImgPath);
	}

	/**
	 * 用户的配置库
	 * 
	 * @param customerId
	 *            客户id
	 * @return 客户资产库页面
	 */
	@RequestMapping("selector/customerCiEdit")
	public ModelAndView customerCiEdit(String customerId, String ctId,
			HttpServletRequest req) {

		ModelAndView mv = new ModelAndView("tags/selector/customerCiEdit");
		mv.addObject("customerId", customerId);
		mv.addObject("ctId", ctId);
		String afterSelected = req.getParameter("afterSelected");
		mv.addObject("afterSelected",
				StringUtils.isEmpty(afterSelected) ? "false	" : afterSelected);

		return mv;
	}
	
	@RequestMapping("selector/categoryServiceSelector")
	public ModelAndView categoryServiceSelector(String ctId, String url,
			String dheight, String idFieldName, HttpServletRequest req) {

		ModelAndView mv = new ModelAndView(
				"tags/selector/categoryServiceSelector");

		mv.addObject("ctId", ctId);
		mv.addObject("dheight", dheight);

		String multiselect = req.getParameter("multiselect");
		mv.addObject("multiselect", multiselect);
		
		String roleName=req.getParameter("roleName");
		mv.addObject("roleName", roleName);

		if (StringUtils.isEmpty(idFieldName)) {
			idFieldName = "name";
		}
		mv.addObject("idFieldName", idFieldName);
		return mv;
	}
}
